/*
 * Patch for error in b = el.getBoundingClientRect()
 * solution found here:
 * https://www.yui-ext.com/forum/showthread.php?t=41178
 *
 **/
Ext.apply(Ext.lib.Dom, {
    getXYFly: function(el) {
        var libFlyweight;
        if (!libFlyweight) {
            libFlyweight = new Ext.Element.Flyweight();
        }
        libFlyweight.dom = el;
        return libFlyweight;
    },
    getXY: function(el) {
        var p, pe, b, scroll, bd = (document.body || document.documentElement);
        el = Ext.getDom(el);
        if(el == bd){
            return [0, 0];
        }

        if (el.getBoundingClientRect) {
            /*
             * start patch: new is the try/catch block
             **/
            var leftB;
            var topB;
            try{
                b = el.getBoundingClientRect();
                leftB = b.left;
                topB = b.top;
            } catch(e){
                leftB = 0;
                topB = 0;
            }
            scroll = this.getXYFly(document).getScroll();
            return [leftB + scroll.left, topB + scroll.top];
            /*
             * end patch
             **/
        }
        var x = 0, y = 0;

        p = el;

        var hasAbsolute = this.getXYFly(el).getStyle("position") == "absolute";

        while (p) {

            x += p.offsetLeft;
            y += p.offsetTop;

            if (!hasAbsolute && this.getXYFly(p).getStyle("position") == "absolute") {
                hasAbsolute = true;
            }

            if (Ext.isGecko) {
                pe = this.getXYFly(p);

                var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
                var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;


                x += bl;
                y += bt;


                if (p != el && pe.getStyle('overflow') != 'visible') {
                    x += bl;
                    y += bt;
                }
            }
            p = p.offsetParent;
        }

        if (Ext.isSafari && hasAbsolute) {
            x -= bd.offsetLeft;
            y -= bd.offsetTop;
        }

        if (Ext.isGecko && !hasAbsolute) {
            var dbd = this.getXYFly(bd);
            x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
            y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
        }

        p = el.parentNode;
        while (p && p != bd) {
            if (!Ext.isOpera || (p.tagName != 'TR' && this.getXYFly(p).getStyle("display") != "inline")) {
                x -= p.scrollLeft;
                y -= p.scrollTop;
            }
            p = p.parentNode;
        }
        return [x, y];
    }
})